<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="角色名称" prop="role_name" required>
        <el-input v-model="form.role_name" placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item style="margin-top: 30px">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button class="bs-left-10" @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/role'

export default {
  components: {

  },
  data() {
    return {
      roleList: [],
      list: [],
      loading: false,
      form: {
        role_id: 0,
        role_name: '',
      }
    }
  },

  mounted() {
    if (this.$route.query.hasOwnProperty('id')) {
      this.getData(this.$route.query.id)
    }
  },

  methods: {
    getData(id) {
      api.info({ id: id }).then((res) => {
        this.form = res.data.info
        this.form.password = ''
      })
    },

    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let req
          if (this.form.role_id > 0) {
            req = api.update(this.form)
          } else {
            req = api.create(this.form)
          }
          req.then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              duration: 500,
              onClose: () => {
                this.$router.go(-1)
              }
            })
          }).catch((error) => {

          })
        }
      })

    }
  }
}
</script>

<style scoped>

.article-textarea ::v-deep {

textarea {
  padding-right: 40px;
  resize: none;
  border: none;
  border-radius: 0px;
  border-bottom: 1px solid #bfcbd9;
}

}
</style>

